package top.superflow.flow.load.factory

import java.io.File

import top.superflow.exception.RequestUnsupportedException
import top.superflow.flow.load._
import top.superflow.flow.load.process.ProcessProviderLoader

class LoaderFactory private() {
  
}

object LoaderFactory {
  
  private val instance = new LoaderFactory 
  
  def getInstance : LoaderFactory = {
    this.instance
  }
  
  def getLoader(file : Any) : Loader[_] = {
    file match {
      case f : File => {
        if(f.isDirectory()){
          throw new RequestUnsupportedException("The parameter should be only file path.")
        }else{
          this.getLoader(f.getName)
        }
      }
      case strf : String => {
        strf match {
          case fname if LoadManager.processProviderFilePattern.matcher(fname).find() =>{
            new ProcessProviderLoader()
          }

          case fname if LoadManager.processFilePattern.matcher(fname).find() =>{
            new ProcessLoader()
          }
          
          case fname if LoadManager.resourceFilePattern.matcher(fname).find() =>{
            new ResourceLoader()
          }
          
          case fname if LoadManager.flowFilePattern.matcher(fname).find() =>{
            new FlowLoader()
          }

          case fname if LoadManager.mapperFilePattern.matcher(fname).find() =>{
            new MapperLoader()
          }

          case fname if LoadManager.templateFilePattern.matcher(fname).find() =>{
            new TemplateLoader()
          }
          
          case _ => {
            throw new RequestUnsupportedException("Can't find any matched loader for the current file name:" + strf)
          }
        }
      }
      
      case _ => {
        throw new RequestUnsupportedException("Can't deal with passed in parameter:" + file)
      }
    }
  }
  
  
  
}

